package com.cade.agriculturemall.view.register;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Log;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVMobilePhoneVerifyCallback;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SignUpCallback;
import com.cade.agriculturemall.model.RegisterEntity;
import com.cade.agriculturemall.base.BasePresenter;
import com.cade.agriculturemall.utils.VerifyUtil;

/**
 * Created by Lobby on 2017/11/15.
 */

public class RegisterPresenter extends BasePresenter<RegisterContract.IView> implements RegisterContract.IPresenter {
    public RegisterPresenter(RegisterContract.IView view) {
        super(view);
    }


    @Override
    public void onResume() {

    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDestroy() {

    }

    /**
     * 在_user表中 添加了一个用户，添加手机号、密码，需引导用户进入RegisterMyCodeActivity。class进行验证码验证。
     * @param mEntity
     */
    @Override
    public void register(RegisterEntity mEntity) {
        if (mEntity != null) {
            if (TextUtils.isEmpty(mEntity.getPhone())) {
                mView.showError("请输入手机号码");
            } else if (TextUtils.isEmpty(mEntity.getPswFirst())) {
                mView.showError("请输入密码");
            } else if (TextUtils.isEmpty(mEntity.getPswTwice())) {
                mView.showError("请再次输入密码");
            } else if (!VerifyUtil.isMobilePhone(mEntity.getPhone())) {
                mView.showError("请输入正确的手机号码");
            } else if (mEntity.getPswFirst().length() < 6) {
                mView.showError("请输入长度最少为6位的密码");
            } else if (!mEntity.getPswFirst().equals(mEntity.getPswTwice())) {
                mView.showError("两次密码输入不一致，请重新输入");
            } else if (!mEntity.getAgree()) {
                mView.showError("请阅读用户协议");
            } else {
                mView.showProgress();
                AVUser user = new AVUser();
                user.setPassword(mEntity.getPswFirst());
                user.setUsername(mEntity.getPhone());
//	注册
                user.put("mobilePhoneNumber", mEntity.getPhone());
                user.signUpInBackground(new SignUpCallback() {
                    public void done(AVException e) {
                        if (e == null) {
                            mView.goRegisterCode();
                        } else {
                            e.printStackTrace();
                        }
                    }
                });

            }
                SharedPreferences sharedPreferences = ((Activity)mView).getSharedPreferences("Time", Context.MODE_PRIVATE);
                sharedPreferences.edit().putLong("time", System.currentTimeMillis()).apply();
                mView.goRegisterCode();
            }
        }


    public void getCode(String code) {
        AVUser.verifyMobilePhoneInBackground(code, new AVMobilePhoneVerifyCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    //

                } else {
                    Log.d("SMS", "Verified failed!");
                }
            }
        });
    }


}
